<?php

namespace StudyBuddy\Api\v1;

use StudyBuddy\Api\Api;

class Users extends Api {

    /**
     * Array of user IDs
     *
     * The list of user IDs can be passed in uids
     * param in a form of semicolon separated values
     * of up to 100 user IDs
     *
     * @var array
     */
    protected $aUids;

    /**
     * Allowed values of the 'sort' param
     *
     * @var array
     */
    protected $allowedSortBy = array('_id', 'i_rep', 'i_lm_ts');

    /**
     * Select ONLY these fields from USERS collection
     *
     * @var array
     */
    protected $aFields = array(
        '_id' => 1,
        'i_rep' => 1,
        'username' => 1,
        'fn' => 1,
        'mn' => 1,
        'ln' => 1,
        'avatar' => 1,
        'avatar_external' => 1,
        'i_reg_ts' => 1,
        'i_lm_ts' => 1,
        'country' => 1,
        'state' => 1,
        'city' => 1,
        'url' => 1,
        'description' => 1
    );

    protected function main() {
        $this->pageID = $this->oRequest['pageID'];

        $this->setSortBy()
                ->setStartTime()
                ->setEndTime()
                ->setUids()
                ->setSortOrder()
                ->setLimit()
                ->getCursor()
                ->setOutput();
    }

    /**
     * If there is a "uids" param in request
     * then use its value to extract 
     * array of uids using the semicolon as separator
     * 
     * Use a maximum of this many ids.
     * 
     * @return object $this
     */
    protected function setUids() {
        $uids = $this->oRequest->get('uids', 's');

        if (!empty($uids)) {
            $this->aUids = explode(';', $uids);
            $total = count($this->aUids);
            if ($total > 100) {
                throw new \StudyBuddy\HttpResponseCodeException('Too many user ids passed in "uids" param. Must be under 100. Was: ' . $total, 406);
            }

            /**
             * IMPORTANT
             * Must cast array elements to 
             * integers, otherwise Mongo will 
             * not be able to find any records because
             * match is type-sensitive!
             */
            array_walk($this->aUids, function(&$item, $key) {
                        $item = (int) $item;
                    });
        }

        return $this;
    }

    protected function getCursor() {
        $sort[$this->sortBy] = $this->sortOrder;
        $offset = (($this->pageID - 1) * $this->limit);
        d('offset: ' . $offset);

        $where = array('role' => array('$ne' => 'deleted'));

        if (isset($this->aUids)) {
            $match = array('$in' => $this->aUids);
            $where['_id'] = $match;
        }

        d('$where: ' . print_r($where, 1));

        $this->cursor = $this->oRegistry->Mongo->USERS->find($where, $this->aFields)
                ->sort($sort)
                ->limit($this->limit)
                ->skip($offset);

        $this->count = $this->cursor->count();
        d('count: ' . $this->count);

        if (0 === $this->count) {
            d('No results found for this query: ' . print_r($where, 1));

            throw new \StudyBuddy\HttpResponseCodeException('No matches for your request', 404);
        }

        return $this;
    }

    /**
     *
     * Set to $this->oOutput object with
     * data from cursor
     *
     * @return object $this
     */
    protected function setOutput() {

        $data = array('total' => $this->count,
            'page' => $this->pageID,
            'perpage' => $this->limit,
            'users' => \iterator_to_array($this->cursor, false));

        $this->oOutput->setData($data);

        return $this;
    }

}
